package com.qf.medical.handler;


import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handlerException(Exception ex) {
        log.error(ex.getMessage());
        return ResponseEntity.error();
    }

    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Object> handlerException(BaseException ex) {
        return ResponseEntity.error(ex.getResponseCode());
    }

    /**
     * 参数类型转换错误
     *
     * @param exception 错误
     * @return 错误信息
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Object> parameterTypeException(MethodArgumentNotValidException exception){
        return ResponseEntity.error(ResponseCode.RESPONSE_ERROR);
    }
}
